﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
#if !PHONE
using System.Windows.Browser;
#endif
using Microsoft.Popfly.GameCreator.GameEngine.Behaviors;

namespace Microsoft.Popfly.GameCreator.GameEngine
{
    public partial class RuntimeBar : UserControl
    {
        private double _width;
        public bool mute
        {
            get
            {
                return Game.IsMuted;
            }
            set
            {
                if (Game.IsMuted ^ value)
                    VolumeButton_Click(null, null);

            }
        }
        public new double Width
        {
            get
            {
                return _width;
            }
            set
            {
                base.Width = value;
                background.Width = value;
                _width = value;
            }
        }
        void InitButton(Button barBtn)
        {
            if (barBtn == null)
            {
                return;
            }
            var ButtonCanvas = barBtn.Content as Canvas;
            if (ButtonCanvas != null)
            {
                var gradients = new GradientStopCollection();

                gradients.Add(new GradientStop()
                {
                    Color = Color.FromArgb(0xFF, 0xF0, 0xA0, 0x30),
                    Offset = 0
                });
                gradients.Add(new GradientStop()
                {
                    Color = Color.FromArgb(0xF0, 0xF0, 0xA0, 0x30),
                    Offset = 0.7
                });
                gradients.Add(new GradientStop()
                {
                    Color = Color.FromArgb(0x00, 0xF0, 0xA0, 0x30),
                    Offset = 1
                });
                var ellipsis = new Ellipse()
                {
                    Width = barBtn.Width + 14,
                    Height = barBtn.Height + 14,
                    Fill = new RadialGradientBrush(gradients)
                };

                ellipsis.SetValue(Canvas.TopProperty, -7d);
                ellipsis.SetValue(Canvas.LeftProperty, -10d);
                ellipsis.SetValue(Canvas.OpacityProperty, 0d);

                ButtonCanvas.Children.Insert(0, ellipsis);
                var fadeIn = new Storyboard();
                var dblAnim = new DoubleAnimation();
                dblAnim.Duration = new Duration(new TimeSpan(2000000));

                Storyboard.SetTarget(dblAnim, ellipsis);
                Storyboard.SetTargetProperty(dblAnim, new PropertyPath(Canvas.OpacityProperty));
                fadeIn.Children.Add(dblAnim);

                barBtn.MouseEnter += delegate(object sender, MouseEventArgs e)
                {
                    dblAnim.From = 0;
                    dblAnim.To = 1;
                    barBtn.SetValue(Canvas.ZIndexProperty, 1);
                    fadeIn.Begin();
                };

                barBtn.MouseLeave += delegate(object sender, MouseEventArgs e)
                {
                    dblAnim.From = 1;
                    dblAnim.To = 0;
                    barBtn.SetValue(Canvas.ZIndexProperty, 0);
                    fadeIn.Begin();
                };
            }
        }
        public RuntimeBar()
        {
            InitializeComponent();
            InitButton(this.PlayButton);
            InitButton(this.PauseButton);
            InitButton(this.Popfly);
            InitButton(this.VolumeButton);
            InitButton(this.HowToPlayButton);
        }
        private Game m_game;
        public Game Game
        {
            get
            {
                return m_game;
            }
            set
            {
                if (m_game != null)
                {
                    m_game.PausedOrUnPaused -= OnPausedUnpausedHandler;
                    m_game.SceneChange -= OnSceneChangeHandler;
                }
                m_game = value;

                if (!Game.m_gameInfo.Scenes.Values.Any((scene) => (scene.Type == "instructions")))
                {
                    this.HowToPlayButton.Visibility = Visibility.Collapsed;
                    this.Resize(this.Width);
                }
                m_game.PausedOrUnPaused += OnPausedUnpausedHandler;
                m_game.SceneChange += OnSceneChangeHandler;
            }
        }
        private void playButtonFaceBackwards()
        {
            if (this.PauseButton.Visibility != Visibility.Visible)
            {
                this.PauseButton.Visibility = Visibility.Visible;
                Storyboard faceForward = this.PlayButton.FindName("faceBackwards") as Storyboard;
                if (faceForward != null)
                {
                    faceForward.Begin();
                }
            }
        }
        private void playButtonFaceForward()
        {
            if (this.PauseButton.Visibility != Visibility.Collapsed)
            {
                this.PauseButton.Visibility = Visibility.Collapsed;
                Storyboard faceBackwards = this.PlayButton.FindName("faceForward") as Storyboard;
                if (faceBackwards != null)
                {
                    faceBackwards.Begin();
                }
            }
        }
        private void UpdateHowToPlayText()
        {
            var howToPlayText = HowToPlayButton.FindName("bigText") as TextBlock;
            if (Game.IsHowToPlayShowing && Game.IsPaused)
            {
                howToPlayText.Text = "go back";
                Canvas.SetLeft(howToPlayText, 11.5d);
            }
            else
            {
                howToPlayText.Text = "how to play";
                Canvas.SetLeft(howToPlayText, 3.5d);
            }
        }
        private void OnPausedUnpausedHandler(Object sender, PausedOrUnPausedArgs args)
        {
            if (Game.CurrentScene.Type == Scene.SceneType.NORMAL && !Game.IsPaused)
            {
                playButtonFaceBackwards();
            }
            else
            {
                playButtonFaceForward();
            }
            UpdateHowToPlayText();
        }

        private void OnSceneChangeHandler(Object sender, SceneChangeEventArgs args)
        {
            var scene = args.newScene;
            if (scene.Type == Scene.SceneType.NORMAL)
            {
                playButtonFaceBackwards();
            }
            else
            {
                playButtonFaceForward();
            }
        }
        internal void Resize(double width)
        {
            this.Width = width;

            double maxBtnWidth = width - LeftAignedButtons.Width;

            double buttonWidth = 0;
            if (VolumeButton.Visibility == Visibility.Visible)
                buttonWidth += VolumeButton.Width;
            if (HowToPlayButton.Visibility == Visibility.Visible)
                buttonWidth += HowToPlayButton.Width;


            RightAlignedButtons.Width = buttonWidth;

            RightAlignedButtons.SetValue(
                Canvas.LeftProperty,
                this.Width - buttonWidth
                );
        }
        private void PopflyButton_Click(object sender, RoutedEventArgs e)
        {
#if !PHONE
            HtmlPage.Window.Eval("window.open('http://www.popfly.com');");
#endif
        }

        private void PauseButton_Click(object sender, RoutedEventArgs e)
        {
            if (Game.IsPaused)
            {
                Game.UnPause();
            }
            else
            {
                Game.Pause();
            }
        }

        private void PlayButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.Game.IsPaused)
            {
                this.Game.UnPause();
            }
            else if (this.Game.CurrentScene.Type != Scene.SceneType.NORMAL)
            {
                //TODO: Move this to scene.cs? possibly?
                var referencedScene = (from behavior in this.Game.CurrentScene.Behaviors
                                       where (behavior is SceneChangeBehavior)
                                       select ((SceneChangeBehavior)behavior).newSceneName).FirstOrDefault();
                if (referencedScene != null)
                {
                    this.Game.ChangeScene(referencedScene);
                }
            }
            else
            {
                this.Game.ChangeScene(this.Game.m_gameInfo.Scenes.getIndexedKey(0));
            }
        }
        private void VolumeButton_Click(object sender, RoutedEventArgs e)
        {
            if (Game.IsMuted)
            {
                crossOut.Visibility = Visibility.Collapsed;
                Game.IsMuted = false;
            }
            else
            {
                crossOut.Visibility = Visibility.Visible;
                Game.IsMuted = true;
            }
        }
        private void HowToPlayButton_Click(object sender, RoutedEventArgs e)
        {
            if (Game.IsHowToPlayShowing)
            {
                Game.UnPause();
            }
            else
            {
                Game.ShowHowToPlay();
            }
        }
    }
}
